package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseThreadLocal;
import com.itheima.reggie.common.R;
import com.itheima.reggie.domain.Orders;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.service.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
    * 去支付，生成订单
    * */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("成功下单");
    }

    /*
    *分页查询订单
    * */
    @GetMapping("/userPage")
    public R<Page> getPage(Integer page, Integer pageSize){
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>(page,pageSize);

        //获取用户id
        Long userId = BaseThreadLocal.get();

        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        qw.eq(Orders::getUserId,userId);
        qw.orderByDesc(Orders::getCheckoutTime);
        Page<Orders> ordersPage = ordersService.page(pageInfo, qw);

        return R.success(ordersPage);
    }
}
